package com.sk.bilibili.model.activity;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.bean.Note;
import com.sk.bilibili.bean.Them;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.dto.DynamicInfo;
import com.sk.bilibili.mapper.ForwardMapper;
import com.sk.bilibili.mapper.NoteMapper;
import com.sk.bilibili.mapper.ThemMapper;
import com.sk.bilibili.mapper.VideoSetMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.view.activity.DynamicActivity;

public class DynamicModel extends BaseModel<DynamicActivity> {

    @Autowired
    VideoSetMapper videoSetMapper;
    @Autowired
    ThemMapper themMapper;
    @Autowired
    NoteMapper noteMapper;
    @Autowired
    ForwardMapper forwardMapper;

    //  0  1   2
    public void loadTarget(Integer type, Integer targetId) {
        switch (type){
            case 0:handleVideoSet(targetId);break;
            case 1:handleThem(targetId);break;
            case 2:handleNote(targetId);break;
        }
    }

    private void handleNote(Integer noteId) {
        RequestUtils.process(noteMapper.getNote(noteId), note -> {
            Member member = note.getExtra().getMember();
            DynamicInfo info = new DynamicInfo();
            info.setName(member.getNickName());
            info.setCover(member.getAvatar());
            if(note.getUrls().length>0)info.setCover(note.getUrls()[0]);
            info.setInfo(note.getContent());
            view.loadTargetSuccess(info);
        });
    }

    private void handleThem(Integer themId) {
        RequestUtils.process(themMapper.getThem(themId), them -> {
            VideoSet videoSet = them.getExtra().getVideoSet();
            DynamicInfo info = new DynamicInfo();
            info.setName(videoSet.getTitle());
            info.setInfo(videoSet.getInfo());
            info.setCover(videoSet.getCover());
            view.loadTargetSuccess(info);
        });
    }

    private void handleVideoSet(Integer videoSetId) {
        RequestUtils.process(videoSetMapper.getVideoSet(videoSetId), videoSet -> {
            Member member = videoSet.getExtra().getMember();
            DynamicInfo info = new DynamicInfo();
            info.setName(member.getNickName());
            info.setInfo(videoSet.getTitle());
            info.setCover(videoSet.getCover());
            view.loadTargetSuccess(info);
        });
    }

    public void pub(Integer type, Integer videoSetId, String content) {
        JsonMap map = new JsonMap();
        map.put("type",type);
        map.put("targetId",videoSetId);
        map.put("content",content);
        RequestUtils.process(forwardMapper.add(map), unused -> view.pubSuccess());
    }

}